package com.zbkj.crmeb.front.service.impl;

import cn.com.antcloud.api.AntFinTechApiClient;
import cn.com.antcloud.api.AntFinTechProfile;
import cn.com.antcloud.api.baasplus.v1_0_0.request.QueryBmpbrowserTransactionqrcodeRequest;
import cn.com.antcloud.api.baasplus.v1_0_0.response.QueryBmpbrowserTransactionqrcodeResponse;
import cn.com.antcloud.api.common.BaseResponse;
import com.alibaba.fastjson.JSONObject;
import com.exception.CrmebException;
import com.utils.CrmebUtil;
import com.utils.QRCodeUtil;
import com.utils.RestTemplateUtil;
import com.zbkj.crmeb.front.service.QrCodeService;
import com.zbkj.crmeb.wechat.service.WeChatService;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * QrCodeServiceImpl 接口实现
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Service
public class QrCodeServiceImpl implements QrCodeService {
    @Autowired
    private WeChatService weChatService;

    @Autowired
    private RestTemplateUtil restTemplateUtil;

    /**
     * 二维码
     *
     * @return Object
     * @author Mr.Zhang
     * @since 2020-05-25
     */
    @Override
    public Map<String, Object> get(JSONObject data) {
        Map<String, Object> map = new HashMap<>();
        StringBuilder scene = new StringBuilder();
        String page = "";
        try {
            if (null != data) {
                Map<Object, Object> dataMap = JSONObject.toJavaObject(data, Map.class);

                for (Map.Entry<Object, Object> m : dataMap.entrySet()) {
                    if (m.getKey().equals("path")) {
                        //前端路由， 不需要拼参数
                        page = m.getValue().toString();
                        continue;
                    }
                    // scene.append(m.getKey()).append("=").append(m.getValue()).append("&");
                    scene.append(m.getValue()).append("&");
                }
            }
        } catch (Exception e) {
            throw new CrmebException("url参数错误 " + e.getMessage());
        }
        String uri = "";
        if (StringUtils.isNotBlank(scene)) {
            uri = scene.substring(0, scene.length() - 1);
        }

        map.put("code", weChatService.qrCode(page, uri));
        return map;
    }

    @Override
    public Map<String, Object> base64(String url) {
        byte[] bytes = restTemplateUtil.getBuffer(url);
        String base64Image = CrmebUtil.getBase64Image(Base64.encodeBase64String(bytes));
        Map<String, Object> map = new HashMap<>();
        map.put("code", base64Image);
        return map;
    }

    /**
     * 讲字符串转为QRcode
     *
     * @param text 待转换字符串
     * @return QRcode base64格式
     */
    @Override
    public Map<String, Object> base64String(String text, int width, int height) {

        String base64Image = null;
        try {
            base64Image = QRCodeUtil.crateQRCode(text, width, height);
        } catch (Exception e) {
            throw new CrmebException("生成二维码异常");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("code", base64Image);
        return map;
    }

    @Override
    public Map<String, Object> getBlockQr(String hash) {
        AntFinTechProfile profile = AntFinTechProfile.getProfile(
                "https://prodapigw.cloud.alipay.com",
                "LTAI4GL5h2TVKGo9vVRay3CK",
                "S86YP5FbDqIUmXNJybt7cVGJuq79dl"
        );
        AntFinTechApiClient client = new AntFinTechApiClient(profile);

        // 构建请求
        QueryBmpbrowserTransactionqrcodeRequest request = new QueryBmpbrowserTransactionqrcodeRequest();
        request.setBizid("a00e36c5");
//        request.setContractId("c740d203a31169da9292599cb3eac01ccae9f13960a40a4f28cd0bcb62ec4de5");
        request.setHash(hash);
        request.setProductInstanceId("plus");

        // 发送请求，并且获取响应结果
        try {
            QueryBmpbrowserTransactionqrcodeResponse response = client.execute(request);
            return new HashMap<String, Object>() {{
                put("qrUrl", response.getQrCodeDownloadUrl());
            }};
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new CrmebException("获取二维码失败");
        }

    }

    public static void main(String[] args) {
        QrCodeServiceImpl a = new QrCodeServiceImpl();

        a.getBlockQr("6762cffb7da798ae2ffd579f29bab75b133bae9de0e374e24b73b5c3c4daa25f");
    }
}

